package com.example.statistics.ui.inventory

import android.os.Bundle
import android.text.method.DigitsKeyListener
import com.example.statistics.R
import com.example.statistics.bean.InventoryBean
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.format
import com.example.statistics.ex.httpPost
import com.example.statistics.ex.isNumber
import com.example.statistics.ex.mulEx
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.product.ProductInfoListActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.prompt.fragment.OptionsFragment
import com.jhj.prompt.fragment.options.interfaces.OnOptionsSelectedListener
import com.example.statistics.ex.afterTextChangedListener
import kotlinx.android.synthetic.main.activity_inventory_add.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.util.*
import kotlin.collections.ArrayList

/**
 * 市场挑货入库
 */
class InventoryAddActivity : BaseActivity() {

    var productId = 0
    var specification = 0.0
    var salesNumber = 0

    override val title: String
        get() = "新增库存"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_inventory_add)
        tv_date.contentText = Date().format().orEmpty()


        tv_product.onContentClicked {
            activityForResult<ProductInfoListActivity>(Constants.ACTION to Constants.SELECTOR) {
                val product = it?.getSerializableExtra(Constants.DATA) as ProductBean?
                if (product != null) {
                    tv_product.contentText = product.factoryNo + product.productName
                    productId = product.id
                    specification = getSpecification(product.specification)
                    if (specification == 0.0) {
                        tv_weight.keyListener = DigitsKeyListener.getInstance("0123456789")
                    } else {
                        tv_weight.keyListener = null
                        tv_weight.contentText = specification.mulEx(salesNumber).toString()
                    }
                }
            }
        }
        tv_warehouse.onClick {
            val optionsOne = arrayListOf<String>("A", "B", "C")
            val a = arrayListOf("1", "2", "3", "4", "5", "6")
            val b = arrayListOf(arrayListOf("1", "2", "3", "4", "5", "6", "7", "8"))
            val options = OptionsFragment.Builder<String>(this@InventoryAddActivity)
            options.setPicker(options1Items = optionsOne, options2Items = optionsOne)
            val optionsTwo = arrayListOf<ArrayList<String>>(a, a, a)
            val optionsThree = arrayListOf<ArrayList<ArrayList<String>>>(b, b, b)
            options.setLinkedPicker(optionsOne, optionsTwo, optionsThree)
            options.setSubmitListener(object : OnOptionsSelectedListener<String> {
                override fun onOptionsSelect(options: OptionsFragment<String>, options1: Int?, options2: Int?, options3: Int?) {

                }
            }).show()
        }

        tv_number.afterTextChangedListener { editable, _ ->
            val price = tv_price.contentText.orEmpty()
            val number = editable.toString()
            tv_weight.contentText = specification.mulEx(number.toDouble()).toString()
            if (tv_weight.keyListener == null && price.isNumber() && number.isNumber()) {
                tv_total_price.contentText = price.toDouble().mulEx(number.toDouble()).toString()
            }
        }
        tv_weight.afterTextChangedListener { editable, _ ->
            val price = tv_price.contentText.orEmpty()
            val weight = editable.toString()
            if (tv_weight.keyListener != null && price.isNumber() && weight.isNumber()) {
                tv_total_price.contentText = price.toDouble().mulEx(weight.toDouble()).toString()
            }
        }
        tv_price.afterTextChangedListener { editable, _ ->
            val price = editable.toString()
            val number = tv_number.contentText.orEmpty()
            val weight = tv_weight.contentText.orEmpty()
            if (tv_weight.keyListener == null && number.isNumber()) { //定装
                tv_total_price.contentText = price.toDouble().mulEx(number.toDouble()).toString()
            } else if (tv_weight.keyListener != null && weight.isNumber()) {//非定装
                tv_total_price.contentText = price.toDouble().mulEx(weight.toDouble()).toString()
            }
        }
        topBarRightText("保存") {
            httpPost<InventoryBean>(UrlConfig.INVENTORY_PURCHASE_ADD, config = {
                isShowDialog = true
                isSuccessToast = true
                listParams = listOf(
                        "id" to "",
                        "productId" to productId,
                        "date" to tv_date.contentText,
                        "supplier" to tv_suppler.contentText,
                        "warehouse" to tv_warehouse.contentText,
                        "purchasePrice" to tv_price.contentText,
                        "purchaseNumber" to tv_number.contentText,
                        "purchaseWeight" to tv_weight.contentText,
                        "totalAmount" to tv_total_price.contentText,
                        "discountAmount" to "",
                        "paymentAmount" to "",
                )
            }) {
                activityFinish()
            }
        }


    }


    private fun getSpecification(str: String?): Double {
        val specification = str?.uppercase(Locale.getDefault())?.replace("KG", "")?.trim()
        return if (specification.isNumber())
            specification?.toDouble() ?: 0.0
        else
            0.0
    }
}